package com.mical.demo.model;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;
import java.util.function.Supplier;


public class User implements UserDetails {


    private Long id;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String username;
    @JsonIgnore
    private String password;
    private String nickname;
    private int enabled;
    private String cloassno;
    private Date regtime;

    private Set<Role> roles;

    public Long getId() {
        return id;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public void setId(Long id) {
        this.id = id;
    }



    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public int getEnabled() {
        return enabled;
    }

    public void setEnabled(int enabled) {
        this.enabled = enabled;
    }

    public String getCloassno() {
        return cloassno;
    }

    public void setCloassno(String cloassno) {
        this.cloassno = cloassno;
    }

    public Date getRegtime() {
        return regtime;
    }

    public void setRegtime(Date regtime) {
        this.regtime = regtime;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        for (Role role : roles) {
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

//    public User(Long id, String username, String password, String nickname, int enabled, String cloassno, Date regtime) {
//        this.id = id;
//        this.username = username;
//        this.password = password;
//        this.nickname = nickname;
//        this.enabled = enabled;
//        this.cloassno = cloassno;
//        this.regtime = regtime;
//

    public User(Long id, String username, String password, String nickname, int enabled, String cloassno, Date regtime,Set<Role> roles) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.nickname = nickname;
        this.enabled = enabled;
        this.cloassno = cloassno;
        this.regtime = regtime;
        this.roles = new HashSet<Role>();

    }

    public Set<Role> getRole() {

        return roles;
    }



}
